// pages/fenlei/fenlei.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsTypeData: [], // 请求到的商品数据
    showData: [], // 渲染数据，带有isActive导航激活属性
    goodsShowData: [], // 商品区域渲染数据
    search_data: [], // 搜索页面搜索内容
    content_height: 667 // 当前页面高度，默认667
  },

  // 初始化页面
  initPage() {
    let pageHeight = 0;
    wx.getSystemInfo({
      success: (res) => {
        pageHeight = res.windowHeight;
        // console.log(pageHeight);
      },
    })
    const query = wx.createSelectorQuery();
    query.select('#searchBox').boundingClientRect();
    query.exec((res) => {
      // console.log(res[0].height);
      let contentHeight = pageHeight - res[0].height;
      // console.log(contentHeight);
      this.setData({
        content_height: contentHeight
      })
    })
  },
  // 获取商品分类数据
  getGoodsType: function() {
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/goodType',
      method: 'POST',
      data: {},
      success: ((res) => {
        // console.log(res.data.data);
        this.setData({
          goodsTypeData: res.data.data
        })
        let goodsdata =  JSON.parse(JSON.stringify(this.data.goodsTypeData));
        let temArr = [];
        for (let i = 0; i < goodsdata.length; i++) { // 遍历每一个数据对象
          if (goodsdata[i].parent_id == 0) {
            let tem = JSON.parse(JSON.stringify(goodsdata[i]));
            tem.children = [];
            temArr.push(tem);
          } else {
            for (let j = 0; j < goodsdata.length; j++) {
              if (goodsdata[i].parent_id === goodsdata[j].good_type_id && i != j) {
                // 判断数组中是否存在该父级
                if (temArr.length == 0) {
                  let temObj = JSON.parse(JSON.stringify(goodsdata[j]));
                  temObj.children = [];
                  temObj.children.push(JSON.parse(JSON.stringify(goodsdata[i])));
                  temArr.push(temObj);
                } else {
                  for (let k = 0; k < temArr.length; k++) {
                    if (temArr[k].good_type_id === goodsdata[j].good_type_id) {
                      temArr[k].children.push(JSON.parse(JSON.stringify(goodsdata[i])));
                      break;
                    } else if (k == temArr.length - 1) {
                      let temObj = JSON.parse(JSON.stringify(goodsdata[j]));
                      temObj.children = [];
                      temObj.children.push(JSON.parse(JSON.stringify(goodsdata[i])));
                      temArr.push(temObj);
                    }
                  }
                }
                break;
              }
            }
          }
        }
        // console.log(temArr);
        for (let i = 0; i < temArr.length; i++) {
          if (i != 0) {
            temArr[i].children.splice(0,1);
          }
        }
        let temParentArr = [];
        let temChildrenArr = [];
        for (let i = 0; i < temArr.length; i++) {
          if (temArr[i].parent_id == 0) {
            let tem = JSON.parse(JSON.stringify(temArr[i]));
            tem.children = [];
            temParentArr.push(tem);
          } else {
            temChildrenArr.push(JSON.parse(JSON.stringify(temArr[i])));
          }
        }
        this.setData({
          search_data: JSON.parse(JSON.stringify(temChildrenArr))
        })
        for (let i = 0; i < temParentArr.length; i++) {
          if (i == 0) {
            temParentArr[i].isActive = true;
          } else {
            temParentArr[i].isActive = false;
          }
          for (let j = 0; j < temChildrenArr.length; j++) {
            if (temChildrenArr[j].parent_id == temParentArr[i].good_type_id) {
              temParentArr[i].children.push(temChildrenArr[j]);
            }
          }
        }
        temArr = temParentArr;
        this.setData({
          showData: temArr
        })
        this.setData({
          goodsShowData: this.data.showData[0].children
        })
        console.log(this.data.showData); // 打印渲染数据

        let search_data_tem = [];
        for (let i = 0; i < this.data.search_data.length; i++) {
          for (let j = 0; j < this.data.search_data[i].children.length; j++) {
            search_data_tem.push(JSON.parse(JSON.stringify(this.data.search_data[i].children[j])));
          }
        }
        // console.log(search_data_tem);
        this.setData({
          search_data: search_data_tem
        })
      }),
      fail: ((err) => {
        console.log(err);
      })
    })
  },
  // 左侧导航栏切换当前样式
  currentGoodsPage(e) {
    const idx = e.currentTarget.dataset.index;
    this.setData({
      goodsShowData: this.data.showData[idx].children
    })
    let list = JSON.parse(JSON.stringify(this.data.showData));
    list.forEach((v,i) => {
      if (i == idx) {
        list[i].isActive = true;
      } else {
        list[i].isActive = false;
      }
    })
    this.setData({
      showData: list
    })
  },
  // 跳转搜索页
  gosearch(e) {
    // console.log('去往搜索模块');
    // console.log(e.currentTarget.dataset);
    // console.log(this.data.search_data);
    // console.log(e);
    if (e.currentTarget.id == '') {
      let title = e.currentTarget.dataset.searchtitle;
      let id = e.currentTarget.dataset.searchid;
      let data = this.data.search_data;
      wx.navigateTo({
        url: '../search/search?search_value=' + title + '&search_id=' + id + '&search_data=' + JSON.stringify(data),
      })
    } else {
      let data = this.data.search_data;
      wx.navigateTo({
        url: '../search/search?search_data=' + JSON.stringify(data),
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initPage();
    this.getGoodsType();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})